from fastapi import FastAPI, HTTPException
from agent import ai_database
import uvicorn

app = FastAPI(title="FastAPI 示例", version="1.0.0")

query_engine = ai_database.init()

# 路由示例
@app.get("/")
async def read_root():
    return {"message": "欢迎使用 DBCat-AI-Server"}

# 路由带参数
@app.get("/ai_answer/{message}")
async def ai_answer(message):
    data = ai_database.getAnswer(query_engine,message)
    return {"code": 200, "success": True, "data": data}

# # 带请求体的 POST
# @app.post("/items/", response_model=Item)
# async def create_item(item: Item):
#     return item

# # 带查询参数的 GET
# @app.get("/products/")
# async def get_products(
#     page: int = Query(1, alias="页码", description="当前页码"),
#     size: int = Query(10, alias="大小", description="每页数量")
# ):
#     return {"page": page, "size": size}

# # 异常处理
# @app.get("/error/{item_id}")
# async def trigger_error(item_id: int = Path(..., description="商品ID")):
#     if item_id < 1:
#         raise HTTPException(status_code=400, detail="商品ID必须大于0")
#     return {"item_id": item_id}

if __name__ == "__main__":
    # 在这里放置启动前执行的代码
    print("服务器即将启动...")
    uvicorn.run(app, host="0.0.0.0", port=8000)
